﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;


// External library used
using Google.Api.Maps.Service;
using Google.Api.Maps.Service.Geocoding;
using Google.Api.Maps.Service.StaticMaps;
using Newtonsoft.Json;

namespace GoogleMaps
{
  public partial class MapPrinter : Form
  {
    private Bitmap DisplayedMap;
    private string CenterSearch;

    GeocodingRequest Geocode = new GeocodingRequest();

    public MapPrinter()
    {
      InitializeComponent();
      CenterSearch = "";


      comboBoxLangage.DataSource    = new BindingSource(Dictionnaire.Language, null);
      comboBoxLangage.DisplayMember = "Value";
      comboBoxLangage.ValueMember   = "Key";
      comboBoxLangage.SelectedIndex = 0;
    }

    private void GetMap()
    {
      //
      //  Get the geocode of the searched address
      //
      var request = new GeocodingRequest();
      request.Address = SearchBox.Text;
      request.Sensor = "false";
      var response = GeocodingService.GetResponse(request);

      var deserializedResponse = response.Results.First();
      string Latitude, Longitude;
      Longitude = deserializedResponse.Geometry.Location.Longitude.ToString("", CultureInfo.GetCultureInfo("en-US"));
      Latitude = deserializedResponse.Geometry.Location.Latitude.ToString("", CultureInfo.GetCultureInfo("en-US")); 

      var newMap = new StaticMap
                     {
                        Sensor    = "false"
                      , Language  = comboBoxLangage.SelectedValue.ToString()
                      , Size      = "640x640"
                      , MapType   = "roadmap"
                      , Zoom      = trackBarGoogleZoom.Value.ToString()
                      , Markers   = Latitude + "," + Longitude
                     };

      string wMapUri = "";
      wMapUri = newMap.ToUri() + "&Scale=2";

      Map.Load(wMapUri);
      DisplayedMap = new Bitmap(Map.Image);
    }

    private Bitmap ZoomImage(Bitmap iOriginalImage, float iZoom)
    {
      var sourceWidth  = iOriginalImage.Width;
      var sourceHeight = iOriginalImage.Height;

      var targetHeight = (int)(Map.Height * iZoom);
      var targetWidth  = (int)(Map.Width  * iZoom);

      int targetTop  = (Map.Height - targetHeight) / 2;
      int targetLeft = (Map.Width  - targetWidth)  / 2;

      // Create a new temporary bitmap to resize the original image

      // The size of this bitmap is the size of the picImage picturebox.

      Bitmap tempBitmap = new Bitmap(Map.Width, Map.Height,
                                     PixelFormat.Format24bppRgb);

      // Set the resolution of the bitmap to match the original resolution.

      tempBitmap.SetResolution(iOriginalImage.HorizontalResolution,
                               iOriginalImage.VerticalResolution);

      // Create a Graphics object to further edit the temporary bitmap

      Graphics bmGraphics = Graphics.FromImage(tempBitmap);

      // First clear the image with the current backcolor

      bmGraphics.Clear(Color.Black);

      // Set the interpolationmode since we are resizing an image here

      bmGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

      // Draw the original image on the temporary bitmap, resizing it using

      // the calculated values of targetWidth and targetHeight.

      bmGraphics.DrawImage(iOriginalImage,
                           new Rectangle(targetLeft, targetTop, targetWidth, targetHeight),
                           new Rectangle(0, 0, sourceWidth, sourceHeight),
                           GraphicsUnit.Pixel);

      // Dispose of the bmGraphics object

      bmGraphics.Dispose();

      // Set the image of the picImage picturebox to the temporary bitmap

      return tempBitmap;
    }

    private void RequestNewStaticMap(object sender, EventArgs e)
    {
      CenterSearch = SearchBox.Text;
      GetMap();
    }

    private void SearchBox_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.Enter)
      {
        CenterSearch = ((TextBox) sender).Text;
        GetMap();
      }
    }

    private void GoogleZoomChanged(object sender, EventArgs e)
    {
      GoogleZoomValue.Text = ((TrackBar) sender).Value.ToString();
      GetMap();
    }

    private void LocalZoomChanged(object sender, EventArgs e)
    {
      LocalZoomValue.Text = ((TrackBar)sender).Value.ToString();
      if(((TrackBar)sender).Value < 10)
      {
        var wZoomedMap = ZoomImage(DisplayedMap, 1 + (((TrackBar)sender).Value) / 10.0f);
        Map.Image = wZoomedMap; 
      }
      else
      {
        ((TrackBar) sender).Value = 1;
        trackBarGoogleZoom.Value++;
        GetMap();
      }
    }
  }
}
